package lock;
/*
同步方法的锁对象：方法前加synchronized关键字
    1.非静态的方法的锁对象是 this
    2.静态的方法的锁对象是 类的字节码文件
 */
public class Test4 {
    public static void main(String[] args) {
        TicketTask4 ticket = new TicketTask4();
        Thread t1 = new Thread(ticket,"窗口1");
        Thread t2 = new Thread(ticket,"窗口2");
        Thread t3 = new Thread(ticket,"窗口3");

        t2.start(); t1.start();
        t3.start();
    }
}
class TicketTask4 implements Runnable{
    private int tickets = 100;
    @Override
    public void run() {
        while(true){
            if (method())
                break;
        }
    }

    private synchronized boolean method() {
        if (tickets==0){
            return true;
        }
        System.out.println(Thread.currentThread().getName()+"卖出了第"+tickets+"张票");
        tickets--;
        return false;
    }
}
